package com.terrynoya.fight.combat
{
	import com.terrynoya.common.util.DisplayUtil;
	import com.terrynoya.fight.animations.AnimationManager;
	import com.terrynoya.fight.animations.IAnimationElement;
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.char.Entity;
	import com.terrynoya.fight.char.ICharaterRender;
	import com.terrynoya.fight.emuns.Facing;
	import com.terrynoya.fight.emuns.PositionType;
	import com.terrynoya.fight.util.Misc;
	import com.terrynoya.geom.MVector2D;
	
	import flash.display.Sprite;
	
	public class Explod extends Entity
	{
		private var _data:ExplodData;
		private var _view:Sprite;
		private var _render:ICharaterRender;
		
		private var _tickCount:int;
		
		private var _forcerRemove:Boolean;
		private var _creationFacing:int;
		
		public function Explod(engine:FightEngine,data:ExplodData)
		{
			super(engine);
			this._data = data;
			this._view = new Sprite();
			this.engine.stage.characterLayer.addChild(this._view);
			this.onCreaterSetted();
		}
		
		override public function updateAnimations():void
		{
			if(this._tickCount > 0)
			{
//				trace("updateAnimations");
//				trace("elementSwitchTime",this.animationManager.elementSwitchTime,this.animationManager.currentElement.id,this.animationManager.currentElement.ticks);
				super.updateAnimations();
			}
		}
		
		override public function updateState():void
		{
			if(this._tickCount == 0)
			{
				this.position = this.getStartLocation();
			}
			this._tickCount ++;
		}
		
		override public function removeCheck():Boolean
		{
			var dataRemoveTime:int = this.data.removeTime;
			if(this._forcerRemove)
			{
				return true;
			}
			else if(dataRemoveTime == -2)
			{
				var isAniFinished:Boolean = this.animationManager.isAniFinished;
				return isAniFinished;
			}
			else if(dataRemoveTime >= 0)
			{
				return this._tickCount > this.data.removeTime;
			}
			else
			{
				return true;
			}
		}
		
		override public function remove():void
		{
			DisplayUtil.removeFromParent(this._view);
		}
		
		public function get data():ExplodData
		{
			return _data;
		}
		
		public function get ticks():int 
		{
			return this._tickCount;
		}

		private function onCreaterSetted():void
		{
			this._creationFacing = this.data.offsetor.facing;
			this._render = this.basePlayer.render;
			this.animationManager = this.basePlayer.animationManager.clone();
			this.setLocalAnimation(this._data.animationId,0);
			this.facing = this.getStartFacing();
		}
		
		private function getStartFacing():int
		{
			var facing:int = Facing.LEFT;
			switch(this.data.positionType)
			{
				case PositionType.P1:
				case PositionType.P2:
				case PositionType.Back:	
				{
					facing = this.offsetor.facing;
					break;
				}
				case PositionType.Front:
				case PositionType.Left:
				case PositionType.Right:
				{
					facing = Facing.RIGHT;
					break;
				}
			}
			return facing;
		}
		
		private function getStartLocation():MVector2D
		{
			var pos:MVector2D;
			switch(this._data.positionType)
			{
				case PositionType.P1:
				case PositionType.P2:
				{
					return Misc.getOffset(this.offsetor.position,this._creationFacing,this.data.position);
					break;
				}
				case PositionType.Left:
				{
					return Misc.getOffset(new MVector2D(this.engine.camera.left,0),Facing.RIGHT,this.data.position);
					break;
				}
				case PositionType.Right:
				{
					return Misc.getOffset(new MVector2D(this.engine.camera.right,0),Facing.RIGHT,this.data.position);
					break;
				}
				case PositionType.Back:
				{
					break;
				}
				case PositionType.Front:
				{
					break;
				}
			}
			return pos; 
		}
		
		public function get basePlayer():Character 
		{
			return this._data.creator;
		}

		override public function draw():void
		{
			this._render.draw(this,this._view);
		}
		
		public function get offsetor():Character
		{
			return this._data.offsetor;
		}

		public function get creator():Character
		{
			return this._data.creator;
		}

	}
}